/**
 * @author 徐楠
 * @date 2022/4/16 17:01
 * @version 1.0
 */

package com.xunan.likou;

public class ValidatePalindromeString {
    public static void main(String[] args) {
        //String s = "A man, a plan, a canal: Panama";
        //String s = "race a car";
        //String s = "0P";//true
        //String s = "OP";//false
        String s = "1a2";//false

        boolean result = isPalindrome(s);
        System.out.println(result);
    }

    public static boolean isPalindrome(String s) {
        //提取字母
        s = s.replaceAll("[^a-z^A-Z^0-9]", "");
        //字母全部转为小写
        s = s.toLowerCase();
        System.out.println(s);
        int length = s.length();
        if (length == 1) {
            return true;
        }
        int index = length - 1;
        for (int i = 0; i < length / 2; i++) {
            char c = s.charAt(index--);
            if (s.charAt(i) != c) {
                return false;
            }
        }
        return true;
    }
}
